package com.app.controller;


import com.alibaba.fastjson.JSONObject;
import com.app.model.SysDic;
import com.app.service.ISysDicService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统配置项 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2025-02-27
 */
@Api(tags = "系统配置项")
@RestController
@RequestMapping("/sysDic")
public class SysDicController {

    @Autowired
    private ISysDicService sysDicService;


    @ApiOperation(value = "根据字典类型查询字典列表", notes = "根据字典类型查询字典列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dicType", value = "字典类型", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/queryDicByType")
    public Result queryDicByType(String dicType) {
        QueryWrapper<SysDic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("type", dicType);
        queryWrapper.orderByAsc("sort");
        List<SysDic> list = sysDicService.list(queryWrapper);
        return Result.success(list);
    }


    @ApiOperation(value = "管理后台---获取字典列表", notes = "管理后台---获取字典列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "字典名称", required = false, dataType = "String", paramType = "query"),
    })
    @GetMapping("/adminGetDicList")
    public Result adminGetDicList(Integer page, Integer limit,String name) {

        Page<SysDic> pageParam = new Page<>(page, limit);
        QueryWrapper<SysDic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        if(name!= null && !name.equals("")){
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByAsc("sort");
        Page<SysDic> pageResult = sysDicService.page(pageParam, queryWrapper);
        JSONObject result = new JSONObject();
        result.put("total", pageResult.getTotal());
        result.put("list", pageResult.getRecords());
        return Result.success(result);
    }



}

